package com.ccc.demoboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccc.demoboot.common.R;
import com.ccc.demoboot.config.JwtUtils;
import com.ccc.demoboot.pojo.*;
import com.ccc.demoboot.service.StorageService;
import com.ccc.demoboot.service.SysuserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wz
 * @Date 2024/5/16 15:02
 * @PackageName:com.ccc.demoboot.controller
 * @ClassName: SysuserController
 */
@RequestMapping("/user")
@RestController
public class SysuserController {


    @Autowired
    SysuserService sysuserService;





    /**
     * 通过JWT Token获取用户信息
     *
     * @param jwttoken 用户的JWT Token，包含用户身份信息
     * @return R 包含用户信息的响应结果
     */
    @PostMapping("/user")
    public R user(@RequestHeader("jwttoken") String jwttoken) {
        // 从JWT Token中解析出用户ID
        Integer jwttoken1 = JwtUtils.getJwtToken(jwttoken, "id");
        // 根据用户ID查询用户信息
        Sysuser byId = this.sysuserService.getById(jwttoken1);
        // 将查询到的用户信息添加到响应结果中并返回
        return new R().add("byId", byId);
    }


    @PostMapping("/upuser")
    public R upUser(@RequestBody Sysuser sysuser) {
        boolean update = this.sysuserService.upUserId(sysuser);
        return new R().add("update", update);
    }


    /**
     * 查询所有用户信息,分页
     *
     * @param page
     * @return
     */
    @GetMapping("/userselect/{page}")
    public R select(@PathVariable("page") int page) {
        //ccc
        PageHelper.startPage(page, 5);//第一个参数表示第几页,第二个参数表示每页的条数
        List<Map<String, Object>> list = this.sysuserService.selectuserdep();
        PageInfo<Map<String, Object>> sysuserPageInfo = new PageInfo<>(list);
        return new R().add("sysuserPageInfo", sysuserPageInfo);
    }


    /**
     * 查询所有信息
     *
     * @return
     */
    @PostMapping("/select")
    public R selectAll() {
        List<Sysuser> list = this.sysuserService.list();
        return new R().add("list", list);
    }


    /**
     * 根据id查询数据
     *
     * @param sysuser
     * @return
     */
    @PostMapping("/upselect")
    public R upselect(@RequestBody Sysuser sysuser) {
        //ccc
        QueryWrapper<Sysuser> sysuserQueryWrapper = new QueryWrapper<>();
        sysuserQueryWrapper.setEntity(sysuser);
        Sysuser one = this.sysuserService.getOne(sysuserQueryWrapper);
        return new R().add("one", one);
    }


    /**
     * 修改数据
     *
     * @param sysuser
     * @return
     */
    @PostMapping("/up")
    public R up(@RequestBody Sysuser sysuser) {
        //ccc
        boolean update = this.sysuserService.updateById(sysuser);
        return new R().add("update", update);
    }

    /**
     * 根据id删除
     *
     * @param sysuser
     * @return
     */
    @PostMapping("/del")
    public R del(@RequestBody Sysuser sysuser) {

//        ccc

        boolean remove = this.sysuserService.removeById(sysuser);
        return new R().add("remove", remove);
    }


    /**
     * 获取父级菜单
     * 根据用户角色查询对应的用户列表，通常用于展示上级菜单或部门信息
     *
     * @param sysuser 请求体，通常包含用户角色等用于查询的参数
     * @return 包含用户列表的响应对象，用于前端展示
     */
    @PostMapping("/selectment")
    public R selectMent(@RequestBody Sysuser sysuser) {
        // 创建查询条件封装类
        QueryWrapper<Sysuser> sysuserQueryWrapper = new QueryWrapper<>();
        // 设置查询条件，查询用户部门/角色与传入参数匹配的记录
        sysuserQueryWrapper.eq("userdep", sysuser.getUserrole());
        // 执行查询并获取结果列表
        List<Sysuser> list = this.sysuserService.list(sysuserQueryWrapper);
        // 构建并返回包含查询结果的响应对象
        return new R().add("list", list);
    }


    /**
     * 添加用户数据
     *
     * @param sysuser 待添加的用户信息，通过请求体传递
     * @return R类型响应，包含添加操作是否成功的标志
     */
    @PostMapping("/add")
    public R add(@RequestBody Sysuser sysuser) {
        // 调用服务层的保存方法，尝试添加用户
        boolean add = this.sysuserService.save(sysuser);
        // 构建并返回响应结果，携带添加操作的成功与否信息
        return new R().add("add", add);
    }

    /**
     * 根据用户名查询用户名
     *
     * @param sysuser
     * @return
     */
    @PostMapping("/searchSelect")
    public R searchSelect(@RequestBody Sysuser sysuser) {
        //ccc
        System.out.println(sysuser.getSysusername());
        List<Map<String, Object>> list = this.sysuserService.searchSelectId(sysuser.getSysusername());
        return new R().add("list", list);
    }

    /**
     * 登录验证
     *
     * @param sysuser
     * @return
     */

    @PostMapping("/login")
    @Transactional // 添加@Transactional注解到方法上
    public R login(@RequestBody Sysuser sysuser) {
        QueryWrapper<Sysuser> sysuserQueryWrapper = new QueryWrapper<>();
        sysuserQueryWrapper.setEntity(sysuser);
        Sysuser one = this.sysuserService.getOne(sysuserQueryWrapper);
        if (one == null) {
            return new R().setCode(502).setMessage("用户名或密码错误");
        } else {//登录成功
            //生成token
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id", one.getId());
            hashMap.put("sysusername", one.getSysusername());
            String jwtToken = JwtUtils.createJwtToken(hashMap);
            return new R().add("jwtToken", jwtToken);
        }
    }



    /**
     * uniAPP登录验证，成功生成jwttoken
     *
     * @return 登录用户信息jwttoken
     */
    @PostMapping("/uniLogin")
    @Transactional // 添加@Transactional注解到方法上
    public R uniLogin(@RequestBody Sysuser sysuser) {
        QueryWrapper<Sysuser> sysuserQueryWrapper = new QueryWrapper<>();
        sysuserQueryWrapper.setEntity(sysuser);
        Sysuser one = this.sysuserService.getOne(sysuserQueryWrapper);
        if (one == null) {
            return new R().setCode(502).setMessage("用户名或密码错误");
        } else {//登录成功
            //生成token
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id", one.getId());
            hashMap.put("sysusername", one.getSysusername());
            String jwtToken = JwtUtils.createJwtToken(hashMap);
            return new R().add("jwtToken", jwtToken).add("user",one);
        }
    }

    /**
     * 验证token
     * @param sysuser
     * @param jwttoken
     * @return
     */
    @PostMapping("/uniCheckLogin")
    public R uniCheckLogin(@RequestBody Sysuser sysuser,@RequestHeader("jwttoken") String jwttoken) {
        //Integer id = JwtUtils.getJwtToken(jwttoken, "id");//获取当前登录用户id
        boolean b = JwtUtils.checkToken(jwttoken);
        System.out.println(b);
        return new R().add("result", b);
    }

    /**
     * uniAPP注销当前用户
     * 根据id删除
     *
     * @param jwttoken
     * @return 删除结果true或false
     */
    @PostMapping("/uniDel")
    public R uniDel(@RequestHeader("jwttoken") String jwttoken) {
        Integer id = JwtUtils.getJwtToken(jwttoken, "id");//获取当前登录用户id
        Sysuser sysuser = new Sysuser();
        sysuser.setId(id);
        boolean remove = this.sysuserService.removeById(sysuser);
        return new R().add("remove", remove);
    }

    /**
     * uniAPP修改密码
     * @param sysuser
     * @param jwttoken
     * @return
     */
    @PostMapping("/uniUpPassword")
    public R uniUpPassword(@RequestBody Sysuser sysuser, @RequestHeader("jwttoken") String jwttoken) {
        Integer id = JwtUtils.getJwtToken(jwttoken, "id");//获取当前登录用户id
        boolean update = sysuserService.upUserPasswordById(sysuser.getSysuserpwd(),id);
        return new R().add("update", update);
    }
}
